package com.lawliet.example.config;

import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.PropertyCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springdoc.core.customizers.OpenApiCustomizer;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * spring doc配置
 */
@Configuration
public class SpringDocConfig {

    @Bean
    public OpenAPI restfulOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("springboot3.x demo")
                        .description("Spring Boot3 Restful API")
                        .version("V1.0.0")
                        .license(new License().name("访问SpringDoc官方网站").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("欢迎访问LDY的技术博客")
                        .url("https://blog.csdn.net/ldy1016"))
                //添加请求认证消息
                .components(new Components()
                        .addSecuritySchemes("Authorization",
                                new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")));
    }

    //为每个请求方法都加上Authorization头
    @Bean
    public OpenApiCustomizer securityOpenApiCustomizer() {
        return openApi -> {
            Paths paths = openApi.getPaths();
            if (paths != null) {
                paths.values().forEach(pathItem ->
                        pathItem.readOperations().forEach(operation ->
                                operation.addSecurityItem(new SecurityRequirement().addList("Authorization"))
                        )
                );
            }
        };
    }
}